package org.adorsys.aderp.aderplogin.web.ext;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.adorsys.aderp.aderplogin.domain.AderpClientDetail;
import org.adorsys.aderp.aderplogin.roles.ContextualRoleVoter;
import org.adorsys.aderp.aderplogin.roles.SimpleContextualAttrEval;
import org.adorsys.aderp.aderplogin.service.AderpClientDetailService;
import org.adorsys.aderp.jpa.security.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

@RequestMapping({"/home", "/", "/index"})
@Controller
public class HomeController {
	
	private SimpleContextualAttrEval rolePPSuport = new SimpleContextualAttrEval("PP_ADERPLOGIN_SUPPORT");
	private SimpleContextualAttrEval rolePPAdmin = new SimpleContextualAttrEval("PP_ADERPLOGIN_ADMIN");

	private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
	
	@Resource
	private AderpClientDetailService aderpClientDetailService;

	public static final String INDEX_PAGE = "index";
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView selectPage(Model uiModel, HttpServletRequest request, HttpServletResponse response) throws IOException{
    	UserDetails userDetails = SecurityUtils.getUserDetails();
    	if(userDetails==null) return index(uiModel);
    	Collection<? extends GrantedAuthority> authorities = userDetails.getAuthorities();
    	for (GrantedAuthority grantedAuthority : authorities) {
			if(ContextualRoleVoter.ACCESS_GRANTED == rolePPSuport.match(grantedAuthority.getAuthority())){
				redirectStrategy.sendRedirect(request, response, "/support");
				return null;
			} else if (ContextualRoleVoter.ACCESS_GRANTED == rolePPAdmin.match(grantedAuthority.getAuthority())){
				redirectStrategy.sendRedirect(request, response, "/admin");
				return null;
			}
		}
    	return index(uiModel);
	}
	
	public ModelAndView index(Model uiModel){
		if(uiModel!=null){
			List<AderpClientDetail> orig = aderpClientDetailService.findAllAderpClientDetails();
			List<AderpClientDetail> clientDetails = new ArrayList<AderpClientDetail>();
			for (AderpClientDetail aderpClientDetail : orig) {
				if(StringUtils.equals("aderp-login", aderpClientDetail.getClientId())) continue;
				clientDetails.add(aderpClientDetail);
			}
			
			uiModel.addAttribute("clientDetails", clientDetails);
		}
    	return new ModelAndView(INDEX_PAGE, uiModel.asMap());
	}
}
